package edu.zqu.instructionalMatters.project.util;

import edu.zqu.instructionalMatters.project.VO.CourseChangesApplicationVO;
import edu.zqu.instructionalMatters.project.entity.Calendar;
import edu.zqu.instructionalMatters.project.entity.CourseChangesApplication;
import edu.zqu.instructionalMatters.project.entity.CourseChangesApplicationStatus;
import edu.zqu.instructionalMatters.project.entity.TimeTable;
import edu.zqu.instructionalMatters.project.mapper.CalendarMapper;
import edu.zqu.instructionalMatters.project.mapper.CourseChangesApplicationStatusMapper;
import edu.zqu.instructionalMatters.project.mapper.CurriculumMapper;
import edu.zqu.instructionalMatters.project.mapper.TimeTableMapper;
import edu.zqu.instructionalMatters.project.service.CollegeService;
import edu.zqu.instructionalMatters.project.service.CourseService;
import edu.zqu.instructionalMatters.project.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author 何文东
 * @Date 2020/7/25 18:37
 * @Description:
 * @Version 1.0
 */
@Component
public class CourseChangesApplicationUtil {

    @Autowired
    private CollegeService collegeService;
    @Autowired
    private UserService userService;
    @Autowired
    private CourseChangesApplicationStatusMapper courseChangesApplicationStatusMapper;
    @Autowired
    private CourseService courseService;
    @Autowired
    private CalendarMapper calendarMapper;
    @Autowired
    private TimeTableMapper timeTableMapper;
    @Autowired
    private CurriculumMapper curriculumMapper;

    public CourseChangesApplicationVO courseChangesApplicationUtil(CourseChangesApplication courseChangesApplication) {
        CourseChangesApplicationVO courseChangesApplicationVO = new CourseChangesApplicationVO();                                             //初始化CourseChangesApplicationVO
        BeanUtils.copyProperties(courseChangesApplication, courseChangesApplicationVO);                                                        //对象复制赋值

        String collegeName = collegeService.getCollegeName(courseChangesApplication.getCollegeId());                                          //获取学校名称
        courseChangesApplicationVO.setCollegeName(collegeName);                                                                               //学校名称

        String teacherName = userService.getUserName(courseChangesApplication.getTeacherId());                                                //获取教师姓名
        courseChangesApplicationVO.setTeacherName(teacherName);                                                                               //教师名称

        CourseChangesApplicationStatus courseChangesApplicationStatus = courseChangesApplicationStatusMapper.selectById(courseChangesApplication.getApplicationType());//获取申请类型名称
        courseChangesApplicationVO.setApplicationTypeName(null);                                                                              //申请类型名称null
        if (courseChangesApplicationStatus != null) {                                                                                            //判断不等于null
            courseChangesApplicationVO.setApplicationTypeName(courseChangesApplicationStatus.getName());                                      //申请类型名称
        }

        String courseName = courseService.getCourseName(curriculumMapper.selectById(courseChangesApplication.getCurriculumId()).getCourseId());//获取课程名称
        courseChangesApplicationVO.setCourseName(courseName);                                                                                  //课程名称

        Calendar calendar = calendarMapper.selectById(courseChangesApplication.getCalendarId());                                               //获取旧校历日期
        courseChangesApplicationVO.setCalendarDate(null);                                                                                      //原校历日期null
        if (calendar != null) {                                                                                                                   //判断不等于null
            courseChangesApplicationVO.setCalendarDate(calendar.getTheDate().toString());                                                      //原校历日期
        }

        Calendar newCalendar = calendarMapper.selectById(courseChangesApplication.getNewCalendarId());                                         //获取新校历日期
        courseChangesApplicationVO.setNewCalendarDate(null);                                                                                   //新校历日期null
        if (newCalendar != null) {                                                                                                                //判断不等于null
            courseChangesApplicationVO.setNewCalendarDate(newCalendar.getTheDate().toString());                                                //新校历日期
        }

        TimeTable timeTable = timeTableMapper.selectById(courseChangesApplication.getTimeTableId());                                           //获取旧作息表名称
        courseChangesApplicationVO.setTimeTableName(null);                                                                                     //原作息名称null
        if (timeTable != null) {                                                                                                                  //判断不等于null
            courseChangesApplicationVO.setTimeTableName(timeTable.getName());                                                                  //原作息名称
        }

        TimeTable newTimeTable = timeTableMapper.selectById(courseChangesApplication.getNewTimeTableId());                                     //获取新作息表名称
        courseChangesApplicationVO.setNewTimeTableName(null);                                                                                  //新作息名称null
        if (newTimeTable != null) {                                                                                                               //判断不等于null
            courseChangesApplicationVO.setNewTimeTableName(newTimeTable.getName());                                                            //新作息名称
        }
        return courseChangesApplicationVO;
    }

}
